---
- hosts: "metrichost"
  remote_user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  gather_facts: yes
  connection: ssh
  
  tasks:
    - name: Download package
      ansible.builtin.get_url:
        url: "{{ metricbeat_package_url }}"
        dest: /home/metricbeat.deb
        mode: '0777'

    - name: Install deb package for metricbeat
      ansible.builtin.command: dpkg -i /home/metricbeat.deb

    - name: Template metricbeat config
      template:
        src: ./templates/metricbeat.yml.j2
        dest: /etc/metricbeat/metricbeat.yml

    - name: metricbeat modules enable system
      ansible.builtin.command: metricbeat modules enable system

    - name: metricbeat setup
      ansible.builtin.command: metricbeat setup

    - name: Ensure metricbeat is running (and enable it at boot)
      ansible.builtin.command: service metricbeat start